/**
 * 
 */
package soa.security.test.ws;

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.junit.Test;

/**
 * @author Cre.Gu
 * 
 */
public class OrganizationServiceClient extends WebServiceClient {
	protected static final org.slf4j.Logger log = org.slf4j.LoggerFactory
			.getLogger(OrganizationServiceClient.class);

	public static final String WSURL = BASE_WSDL + "OrganizationService?wsdl";

	public Boolean addSubOrganization(String organizationId, String organization) {
		try {
			String method = "addSubOrganization";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("organization", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.setReturnType(XMLType.XSD_BOOLEAN);

			Object obj = call.invoke(new Object[] { organizationId,
					organization });
			Boolean retData = (Boolean) obj;
			return retData;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	public Boolean updateOrganization(String organization) {
		try {
			String method = "updateOrganization";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法

			call.addParameter("organization", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.setReturnType(XMLType.XSD_BOOLEAN);

			Object obj = call.invoke(new Object[] { organization });
			Boolean retData = (Boolean) obj;
			return retData;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public String allSubOrganizations(String organizationId) {
		try {
			String method = "allSubOrganizations";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.setReturnType(XMLType.XSD_STRING);

			Object obj = call.invoke(new Object[] { organizationId });
			String retData = (String) obj;
			return retData;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public String getOrganization(String organizationId) {
		try {
			String method = "getOrganization";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.setReturnType(XMLType.XSD_STRING);

			return (String) call.invoke(new Object[] { organizationId });
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public String allOrganization() {
		try {
			String method = "allOrganization";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法
			
			call.setReturnType(XMLType.XSD_STRING);

			Object obj = call.invoke(new Object[] {  });
			String retData = (String) obj;
			return retData;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Test
	public void testAddSub() throws Exception {
		addSubOrganization(ORGANIZATIONID,
				WebServiceClient.readXml("organization.xml"));
	}

	@Test
	public void testUpdate() throws Exception {
		updateOrganization(WebServiceClient.readXml("organization.xml"));
	}

	@Test
	public void testOther() throws Exception {

		System.out.println(allSubOrganizations(ORGANIZATIONID));

		System.out.println(getOrganization("OR0009"));
		
		System.out.println(allOrganization());
	}
}
